package com.wgj.android.http.weixin;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.jiaai.xui.XUI;
import com.jiaai.xui.cache.XCache;
import com.wgj.android.entity.Friend;
import com.wgj.android.entity.WeiXingGroup;
import com.wgj.android.http.weixin.model.CleanInfo;
import com.wgj.android.http.weixin.model.SyncKey;
import com.wgj.android.http.weixin.model.WXFriend;
import com.wgj.android.http.weixin.model.WXInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2017/11/26.
 */

public class WeiXinCacheUtil {
    public  static  final  String CLEAN_INFO="cleans";
    public static  final  String WXINFO_CACHE="wxinfo";
    public static  final  String COOKIE_CACHE="cookie";
    public static final String SYNCKEY_CACHE="synckey";
    public static final String STATUS_CACHE="status";
    public static  final String CHAT_GRPUP_KEY="chat_group";


    /**
     * 获取已检测的朋友
     * @param context
     */
    public  static ArrayList<String>  getCleans(Context context){
        ArrayList<String> cleans=(ArrayList<String>) XCache.get(context).getAsObject(CLEAN_INFO);
        if(cleans==null){
            return  new ArrayList<>();
        }
      return cleans;
    }

    /**
     * 保存已检测的朋友
     * @param context
     * @param cleans
     */
    public  static void  saveCleans(Context context,ArrayList<String> cleans){
         XCache.get(context).put(CLEAN_INFO,cleans);
    }


    /**
     * 获取微信群加好友的记录
     * @param context
     */
    public  static  HashSet<String> getAddFirendsByGroup(Context context){
        HashSet<String> datas= (HashSet<String>) XCache.get(context).getAsObject("AddFirendsByGroup");
        if(datas==null){
            datas=new HashSet<>();
        }
        return datas;

    }

    /**
     * 绶存微信群加好友的记录
     * @param context
     * @param friends
     */
    public  static  void  saveAddFriendsByGroup(Context context,HashSet<String> friends){
        XCache.get(context).put("AddFirendsByGroup",friends);
    }

    /**
     * 获取附近加好友的记录
     * @param context
     */
    public  static  ArrayList<String> getAddFirendsByLocation(Context context){
        ArrayList<String> datas= (ArrayList<String>) XCache.get(context).getAsObject("AddFriendsByLocation");
        if(datas==null){
            datas=new ArrayList<>();
        }
        return datas;

    }

    /**
     * 绶存附近加好友的记录
     * @param context
     * @param friends
     */
    public  static  void  saveAddFriendsByLocation(Context context,ArrayList<String> friends){
        XCache.get(context).put("AddFriendsByLocation",friends);
    }


    public  static  void saveInfo(WXInfo wxInfo){
        XCache.get(XUI.getContext()).put(WXINFO_CACHE,wxInfo);
    }

    public  static  WXInfo getInfo(){
        WXInfo wxInfo=(WXInfo) XCache.get(XUI.getContext()).getAsObject(WXINFO_CACHE);
        if(wxInfo==null){
            return  new WXInfo();
        }
        return  wxInfo;
    }

    public static void saveSynckey(String str){
        SyncKey syncKey = JSONObject.parseObject(str.toString(), SyncKey.class);
        XCache.get(XUI.getContext()).put(SYNCKEY_CACHE, syncKey);
    }

    public static SyncKey getSynckey(){
        return (SyncKey) XCache.get(XUI.getContext()).getAsObject(SYNCKEY_CACHE);
    }

    public  static  void saveCookie(String cookie){
        XCache.get(XUI.getContext()).put(COOKIE_CACHE,cookie);
    }
    public  static  String getCookie(){
       return    XCache.get(XUI.getContext()).getAsString(COOKIE_CACHE);
    }
    public  static  void removeCookie(){
            XCache.get(XUI.getContext()).remove(COOKIE_CACHE);
    }

    public static void removeAll(){
        XCache.get(XUI.getContext()).remove(WXINFO_CACHE);
        XCache.get(XUI.getContext()).remove(COOKIE_CACHE);
        XCache.get(XUI.getContext()).remove(SYNCKEY_CACHE);
    }

    public static int getStatus(){
        Object o = XCache.get(XUI.getContext()).getAsObject(STATUS_CACHE);
        return o == null? -100 : (int)o;
    }

    public static void saveStatus(int status){
        XCache.get(XUI.getContext()).put(STATUS_CACHE, status);
    }

    public static void clearInfo(){
        XCache.get(XUI.getContext()).remove(WXINFO_CACHE);
    }


    public  static void saveChatGroup(String uuId,ArrayList<String> groups){
        XCache.get(XUI.getContext()).put(CHAT_GRPUP_KEY+"_"+uuId,groups);
    }
    public  static ArrayList<String> ChatGroup(String uuId){
       return (ArrayList<String>) XCache.get(XUI.getContext()).getAsObject(CHAT_GRPUP_KEY+"_"+uuId);
    }

    public  static CleanInfo getCleanInfo(String uuId){
        CleanInfo cleanInfo= (CleanInfo) XCache.get(XUI.getContext()).getAsObject(CLEAN_INFO+"_"+uuId);
        if(cleanInfo==null){
            cleanInfo=new CleanInfo();
            cleanInfo.setLastCleanTime(new Date().getTime());
            saveCleanInfo(uuId,cleanInfo);
        }
        return cleanInfo;
    }
    public  static void saveCleanInfo(String uuId,CleanInfo info){
          XCache.get(XUI.getContext()).put(CLEAN_INFO+"_"+uuId,info);
    }

    public  static void  saveWeiXinGroup(String uuid,ArrayList<WeiXingGroup> weiXingGroups){
        XCache.get(XUI.getContext()).put("WeiXinGroup_"+uuid,weiXingGroups);
    }
    public  static List<WeiXingGroup> getWeiXinGroups(String uuid){
          return (List<WeiXingGroup>) XCache.get(XUI.getContext()).getAsObject("WeiXinGroup_"+uuid);
    }


    public  static  void saveFriends(String uuId,ArrayList<WXFriend> friends){
        XCache.get(XUI.getContext()).put("Friends_"+uuId,friends);
    }
    public  static List<WXFriend> getFriends(String uuId){
        return (List<WXFriend>) XCache.get(XUI.getContext()).getAsObject("Friends_"+uuId);
    }
}
